package com.wawayaya.bookreader.bean;

import android.text.TextUtils;
import java.util.ArrayList;
import com.wawayaya.bookreader.DefaultSettings;
import com.wawayaya.util.TLog;

/**
 * 用于显示当前页数据
 *
 */
public class Pagedata implements java.io.Serializable {
	public  String  str="fff";
	private static final long serialVersionUID = 1L;
	
	// 1. book_page; 2. book_content; 3. forecover; 4. backcover
	private int m_ipagetype = DefaultSettings.ms_Json_BookPageType_unknown;
	
	private StringIntDouble m_fullcnfile = null;
	private StringIntDouble m_fullcnpyfile = null;
	private StringIntDouble m_fullcnenfile = null;
	private StringIntDouble m_fullcnpyenfile = null;
	private StringIntDouble m_sforecnfile = null;
	
	private StringIntQuadruple[] m_bkgfiles = null;
	private StringIntQuadruple[] m_cnfiles = null;
	private ArrayList<StringIntQuadruple> m_cnfilelist = null;
	private StringIntQuadruple[] m_kokcnfiles = null;
    private StringIntQuadruple[] m_kokenfiles = null;
	private StringIntQuadruple[] m_pyfiles = null;
	private StringIntQuadruple[] m_enfiles = null;
    private ArrayList<StringIntQuadruple> m_enfilelist = null;
	
	private IntDouble m_appscreensize = null;
	private String m_stext = null;
	private DictRegion m_dr = null;
	private KokRegion m_kr = null;
	private String mTitleMp3 = null;

    private String mTrans = "";
    private boolean   mHasPinyin = false;
    private boolean   mHasEn = false;
    private boolean   mHasKok = false;
    private boolean   mHasCn = false;


    private boolean   mEnglishPage = false;

    private ArrayList<HotRegionData> mHotRegionDataList;
	
	private ArrayList<ArrayList<RectSerialBoolean>> m_alposition = new ArrayList<ArrayList<RectSerialBoolean>>();
	
	public IntDouble getapplictionscreensize(){
		return m_appscreensize.clone();
	}

	public IntDouble getbackgroundimgsize(){
		if(null != m_fullcnfile) {
			return new IntDouble(m_fullcnfile.m_ival1, m_fullcnfile.m_ival2);
		}
		else if(null != m_fullcnpyfile) {
			return new IntDouble(m_fullcnpyfile.m_ival1, m_fullcnpyfile.m_ival2);
		}
		return null;
	}
	
	public int getpageType() {
		return m_ipagetype;
	}
	
	public IntDouble getforegroundimgsize(){
		if(null != m_sforecnfile) {
			return new IntDouble(m_sforecnfile.m_ival1, m_sforecnfile.m_ival2);
		}
		return null;
	}
	
	public String getwgcn(){
		try{
			return m_fullcnfile.m_sval1;
		}catch(Exception e){
		}
		return null;
	}

	public String getwgcnpy(){
		try{
			return m_fullcnpyfile.m_sval1;
		}catch(Exception e){
		}
		return null;
	}
	
	public String getwgcnen(){
		try{
			return m_fullcnenfile.m_sval1;
		}catch(Exception e){
		}
		return null;
	}
	
	public String getwgcnpyen(){
		try{
			return m_fullcnpyenfile.m_sval1;
		}catch(Exception e){
		}
		return null;
	}
	
	public String getfgcn(){
		try{
			return m_sforecnfile.m_sval1;
		}catch(Exception e){
		}
		return null;
	}
	
	public StringIntQuadruple[] getbkgsimgs() {
		return m_bkgfiles;
	}
	
	public StringIntQuadruple[] getcnsimgs() {
		return m_cnfiles;
	}
	
	public ArrayList<StringIntQuadruple> getcnsimglist() {
		return m_cnfilelist;
	}

    public ArrayList<StringIntQuadruple> getensimglist() {
        return m_enfilelist;
    }

	
	public int getcnsimgsSize() {
		if (m_cnfiles != null) {
			return m_cnfiles.length;
		}
		return 0;
	}

    public int getensimgsSize() {
        if (m_enfiles != null) {
            return m_enfiles.length;
        }
        return 0;
    }

	
	public StringIntQuadruple[] getpysimgs() {
		return m_pyfiles;
	}
	
	public StringIntQuadruple[] getensimgs() {
		return m_enfiles;
	}
	
	public DictRegion getDictWordRegion() {
		return m_dr;
	}

	public KokRegion getKokRegion() {
		return m_kr;
	}
	
	public void settype(int itype) {
		m_ipagetype = itype;
	}
	
	public void updatebackgroundCNPY(String sfullpath, int w, int h) {
		m_fullcnpyfile = null;
		m_fullcnpyfile = new StringIntDouble(sfullpath, w, h);
	}

	public void updatebackgroundCN(String sfullpath, int w, int h) {
		m_fullcnfile = null;
		m_fullcnfile = new StringIntDouble(sfullpath, w, h);
	}
	
	public void updatebackgroundCNEN(String sfullpath, int w, int h) {
		m_fullcnenfile = null;
		m_fullcnenfile = new StringIntDouble(sfullpath, w, h);
	}
	
	public void updatebackgroundCNPYEN(String sfullpath, int w, int h) {
		m_fullcnpyenfile = null;
		m_fullcnpyenfile = new StringIntDouble(sfullpath, w, h);
	}
	
	public void updateforegroundCN_2(String sfullpath, int w, int h) {
		m_sforecnfile = null;
		m_sforecnfile = new StringIntDouble(sfullpath, w, h);
	}

	public void addbackgrounds_1(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h) {
		if(null == m_bkgfiles && itotalcount > 0) {
			m_bkgfiles = new StringIntQuadruple[itotalcount];
		}
		if(iidx < m_bkgfiles.length) {
			m_bkgfiles[iidx] = null;
			m_bkgfiles[iidx] = new StringIntQuadruple(sfullpath, ileft, itop, w, h);
		}
	}
	
	public void addbackgroundCNs_3(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
		if(null == m_cnfiles && itotalcount > 0) {
			m_cnfiles = new StringIntQuadruple[itotalcount];
		}
		if(iidx < m_cnfiles.length) {
			m_cnfiles[iidx] = null;
			m_cnfiles[iidx] = new StringIntQuadruple(sfullpath, ileft, itop, w, h);
		}

        if (m_cnfiles.length > 0) {
            mHasCn = true;
        }
	}
	
	public void addforeKoKCNs_3(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
		if(null == m_kokcnfiles && itotalcount > 0) {
			m_kokcnfiles = new StringIntQuadruple[itotalcount];
		}
		if(iidx < m_kokcnfiles.length) {
			m_kokcnfiles[iidx] = null;
			m_kokcnfiles[iidx] = new StringIntQuadruple(sfullpath, ileft, itop, w, h);
		}
	}

    public void addforeKoKENs_3(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
        if(null == m_kokenfiles && itotalcount > 0) {
            m_kokenfiles = new StringIntQuadruple[itotalcount];
        }
        if(iidx < m_kokenfiles.length) {
            m_kokenfiles[iidx] = null;
            m_kokenfiles[iidx] = new StringIntQuadruple(sfullpath, ileft, itop, w, h);
        }
    }

	
	public StringIntQuadruple[] getkokcnsimgs() {
		return m_kokcnfiles;
	}

    public StringIntQuadruple[] getkokensimgs() {
        return m_kokenfiles;
    }

	
	public int getkokcnsimgsSize() {
		if (m_kokcnfiles != null) {
			return m_kokcnfiles.length;
		}
		return 0;
	}
	
	
    public void addbackgroundCNs_3_new(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
		if(null == m_cnfilelist && itotalcount > 0) {
			m_cnfilelist = new ArrayList<StringIntQuadruple>();
			
		}
		if(iidx < itotalcount) {
			//m_cnfiles[iidx] = new StringIntQuadruple(sfullpath, ileft, itop, w, h);
			m_cnfilelist.add(new StringIntQuadruple(sfullpath, ileft, itop, w, h));
		}

        if (m_cnfilelist.size() > 0) {
            mHasCn = true;
        }
	}

    public void addbackgroundEns_3_new(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
        if(null == m_enfilelist && itotalcount > 0) {
            m_enfilelist = new ArrayList<StringIntQuadruple>();

        }
        if(iidx < itotalcount) {
            m_enfilelist.add(new StringIntQuadruple(sfullpath, ileft, itop, w, h));
        }

        if (m_enfilelist.size() > 0) {
            mHasEn = true;
        }
    }

	
	public void addbackgroundPYs_3(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
		if(null == m_pyfiles && itotalcount > 0) {
			m_pyfiles = new StringIntQuadruple[itotalcount];
		}
		if(iidx < m_pyfiles.length) {
			m_pyfiles[iidx] = null;
			m_pyfiles[iidx] = new StringIntQuadruple(sfullpath, ileft, itop, w, h);
		}

        if (m_pyfiles.length > 0) mHasPinyin = true;
	}

    public boolean hasPinyin() { return mHasPinyin;}
	
	public void addbackgroundENs_3(int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
		if(null == m_enfiles && itotalcount > 0) {
			m_enfiles = new StringIntQuadruple[itotalcount];
		}
		if(iidx < m_enfiles.length) {
			m_enfiles[iidx] = null;
			m_enfiles[iidx] = new StringIntQuadruple(sfullpath, ileft, itop, w, h);
		}

        if (m_enfiles.length > 0) {
            mHasEn = true;
        }
	}

    public boolean hasEn() {return mHasEn;}
    public boolean hasCn() {return mHasCn;}
		
	public void updatesentencetext(String stext) {
		m_stext = stext;
	}
	
	public void setTitleMp3(String title_mp3) {
		mTitleMp3 = title_mp3;
	}
	
	public String getTitleMp3() {
		return mTitleMp3;
	}
	
	public void setapplictionscreensize_1(int w, int h) {
		m_appscreensize = null;
		m_appscreensize = new IntDouble(w, h);
	}
	
	public void updatesentencetrectlines_3(int isentenceid, StringDouble[] sds, int iscreenwidth, int iscreenheight, boolean m_bcalculated){
		float fratio = 1.0f;
		if(m_bcalculated) {
			try {
				fratio = DefaultSettings.getToScreenRatio(iscreenwidth, iscreenheight,
						m_sforecnfile.m_ival1, m_sforecnfile.m_ival2, 
						m_sforecnfile.m_ival1, m_sforecnfile.m_ival2);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		int iupperbound = m_alposition.size() - 1;
		if(isentenceid <= iupperbound) {
			m_alposition.remove(isentenceid);
		}
		ArrayList<RectSerialBoolean> alrs = new ArrayList<RectSerialBoolean>();
		for(StringDouble sd : sds) {
			try{
				String srect[] = sd.m_sVal1.split(",");
				if(4 != srect.length) {
					continue;
				}
				int ioffet = Integer.parseInt(srect[0]);
				int itop = Integer.parseInt(srect[1]);
				int irectend = Integer.parseInt(srect[2]) - ioffet;
				int ibottom = Integer.parseInt(srect[3]);
				String slines[] = ("0," + (TextUtils.isEmpty(sd.m_sVal2) ? "" : sd.m_sVal2 + ",") + irectend).split(",");
				for(int i = 0; i < slines.length - 1; i++) {
                    RectSerialBoolean rs = new RectSerialBoolean(Integer.parseInt(slines[i]) + ioffet, itop, Integer.parseInt(slines[i + 1]) + ioffet, ibottom, false);
					rs.doratio(fratio);
					alrs.add(rs);
				}
			}catch(Exception e){
			}
		}
		if(alrs.size() > 0){
			m_alposition.add(isentenceid, alrs);
		}	
	}
	
	public void updatesentencetrectlines_3_new(int isentenceid, StringDouble[] sds, int iscreenwidth, int iscreenheight, boolean m_bcalculated){
		float fratio = 1.0f;
		if(m_bcalculated) {
			try {
				fratio = DefaultSettings.getToScreenRatio(iscreenwidth, iscreenheight,
						m_bkgfiles[0].m_iVal4, m_bkgfiles[0].m_iVal5, 
						m_bkgfiles[0].m_iVal4, m_bkgfiles[0].m_iVal5);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		int iupperbound = m_alposition.size() - 1;
		if(isentenceid <= iupperbound) {
			m_alposition.remove(isentenceid);
		}
		ArrayList<RectSerialBoolean> alrs = new ArrayList<RectSerialBoolean>();
		int index = 0;
		for(StringDouble sd : sds) {
			try{
				String srect[] = sd.m_sVal1.split(",");
				if(4 != srect.length) {
					index++;
					continue;
				}
				StringIntQuadruple cur_cn = null;
                if (!mEnglishPage) {
                    if (m_cnfiles != null && m_cnfiles.length == sds.length) {
                        cur_cn = m_cnfiles[index];
                    }
                } else {
                    if (m_enfiles != null && m_enfiles.length == sds.length) {
                        cur_cn = m_enfiles[index];
                    }
                }
			   
				int ioffet = Integer.parseInt(srect[0]);
				int itop = Integer.parseInt(srect[1]);
				int irectend = Integer.parseInt(srect[2]) - ioffet;
				int ibottom = Integer.parseInt(srect[3]);
				
				int cn_left = ioffet;
				int cn_top =  itop;
				int cn_width = irectend;
				int cn_height = ibottom - itop;
				
				if (cur_cn != null) {
					cn_left = cur_cn.m_iVal2;
					cn_top = cur_cn.m_iVal3;
					cn_width = cur_cn.m_iVal4;
					cn_height = cur_cn.m_iVal5;
				}
				itop = cn_top;
				ibottom = cn_top + cn_height;
				
				int cn_i_offset_left =  cn_left - ioffet;
				irectend = cn_width + cn_i_offset_left;
				
				//String slines[] = ("0," + (TextUtils.isEmpty(sd.m_sVal2) ? "" : sd.m_sVal2 + ",") + irectend).split(",");
				String slines[] = (String.valueOf(cn_i_offset_left) + ","+ (TextUtils.isEmpty(sd.m_sVal2) ? "" : sd.m_sVal2 + ",") + String.valueOf(irectend)).split(",");
				for(int i = 0; i < slines.length - 1; i++) {
					//RectSerial rs = new RectSerial(Integer.parseInt(slines[i]) + ioffet, itop, Integer.parseInt(slines[i + 1]) + ioffet, ibottom);
                    RectSerialBoolean rs;
                    if (i == 0) {
                        rs = new RectSerialBoolean(Integer.parseInt(slines[i]) - cn_i_offset_left + cn_left, itop, Integer.parseInt(slines[i + 1]) - cn_i_offset_left + cn_left, ibottom, true);
                    } else {
                        rs = new RectSerialBoolean(Integer.parseInt(slines[i]) - cn_i_offset_left + cn_left, itop, Integer.parseInt(slines[i + 1]) - cn_i_offset_left + cn_left, ibottom, false);
                    }
					//rs.doratio(fratio);
					alrs.add(rs);
				}
				index++;
			}catch(Exception e){
			}
		}
		if(alrs.size() > 0){
			m_alposition.add(isentenceid, alrs);
		}	
	}
	
	
	public void updatesentencetcutword_4(int isentenceid, StringDouble[] sds){
		int isize = m_alposition.size();
		if(isize <= 0) {
			return;
		}
		isize = m_alposition.get(isentenceid).size();
		if(isize <= 0) {
			return;
		}
		ArrayList<StringTriple> alt = new ArrayList<StringTriple>();
		for(StringDouble sd : sds){
			try{
                if (!mEnglishPage) {
                    int ilen = sd.m_sVal1.length();
                    for(int i = 0; i < ilen; i++) {
                        StringTriple st = new StringTriple(sd.m_sVal1.charAt(i), sd.m_sVal1, sd.m_sVal2);
                        alt.add(st);
                    }
                } else {
                    String[] words = sd.m_sVal1.split(" ");
                    int ilen = words.length;
                    for(int i = 0; i < ilen; i++) {
                        StringTriple st = new StringTriple(words[i], sd.m_sVal1, sd.m_sVal2);
                        alt.add(st);
                    }
                }
			}catch(Exception e){
			}
		}
		if(alt.size() > isize) {
			TLog.log("Pagedata : updatesentencetcutword_4 : " + alt.size() + " != " + isize);
			return;
		}
		boolean bnewone = true;
		if(null != m_dr) {
			Integer ii = (Integer)m_dr.gettag();
			if(null != ii && isentenceid > 0 && ii.intValue() == isentenceid - 1) {
				bnewone = false;
			}
		}
		if(bnewone) {
			m_dr = new DictRegion();
		}
		m_dr.settag(isentenceid);
        int len  = alt.size();
		for(int i = 0; i < len; i++) {
			RectSerialBoolean rs = m_alposition.get(isentenceid).get(i);
			m_dr.add(isentenceid, rs, alt.get(i).m_sVal1, alt.get(i).m_sVal2, alt.get(i).m_sVal3);
		}
	}
	
	public void updateaudiopathtime_5(int isentenceid, String smp3, StringIntDouble[] sids) {
		int isize = m_alposition.size();
		if(isize <= 0) {
			return;
		}
		isize = m_alposition.get(isentenceid).size();
		if(isize <= 0) {
			return;
		}
		if(sids.length < isize) {
			TLog.log("Pagedata : updateaudiopathtime_3 : < " + isize);
			return;
		}
		if(null == m_kr) {
			m_kr = new KokRegion();
		}
        mHasKok = true;
		int iaudiobegin = sids[0].m_ival1, iaudioend = sids[sids.length - 1].m_ival2;
        //added by wang qian below
        ArrayList<StringIntDouble> temp_sids = new ArrayList<StringIntDouble>();
        int j = 0;
            for (StringIntDouble  obj: sids) {
            if (!TextUtils.isEmpty(sids[j].m_sval1)) {
                temp_sids.add(sids[j]);
            } else if (TextUtils.isEmpty(sids[j].m_sval1) && j == sids.length - 1) {
                    temp_sids.add(sids[j]);
                }
           j++;
        }
        int temp_sids_size = temp_sids.size();
         //added by wang qian above
		RectSerialIntDouble[] rsid = new RectSerialIntDouble[sids.length];
        /*		int idx = 0;
		for(int i = 0; i < sids.length; i++) {
			if(TextUtils.isEmpty(sids[i].m_sval1) && i > 0) {
				rsid[i] = new RectSerialIntDouble(rsid[i - 1].m_rc, 
												sids[i].m_ival1, sids[i].m_ival2);
			}
			else{
				if(idx >= isize) {
					rsid = null;
					return;
				}
				rsid[i] = new RectSerialIntDouble(
						m_alposition.get(isentenceid).get(idx), 
						sids[i].m_ival1, sids[i].m_ival2);				
				idx++;
			}
		}*/

        for (int i = 0; i < isize; i++) {
            rsid[i] = new RectSerialIntDouble(
                    m_alposition.get(isentenceid).get(i),
                    temp_sids.get(i).m_ival1, temp_sids.get(i).m_ival2);
        }

		m_kr.add(isentenceid, rsid, smp3, iaudiobegin, iaudioend);
	}
	
	public boolean updatecharacterposition(int iscreenwidth, int iscreenheight) {
		if(null == m_alposition) {
			return false;
		}
		float fratio = 1.0f;
		try {

            fratio = DefaultSettings.getToScreenRatio(iscreenwidth, iscreenheight,
                    m_bkgfiles[0].m_iVal4, m_bkgfiles[0].m_iVal5,
                    m_bkgfiles[0].m_iVal4, m_bkgfiles[0].m_iVal5);

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		for(ArrayList<RectSerialBoolean> alrs : m_alposition) {
			if(null != alrs) {
				for(RectSerialBoolean rs : alrs){
					rs.doratio(fratio);
				}
			}
			else{
				return false;
			}
		}
		return true;
	}

    public void setTrans(String trans)
    {
        mTrans = trans;
    }

    public String getTrans() {return mTrans;}

    public boolean hasKok() {return mHasKok;}

    public void updateHotRegionData(ArrayList<HotRegionData> list) {
        mHotRegionDataList = list;
    }

    public ArrayList<HotRegionData> getHotRegionData() {
        return mHotRegionDataList;
    }

    public boolean getEnglishPage() {
        return mEnglishPage;
    }

    public void setEnglishPage(boolean englishPage)
    {
        mEnglishPage = englishPage;
    }


}
